// 记录发送请求次数
let ajaxCount = 0
export const api = (options) => {
  const baseUrl = 'https://api-hmugo-web.itheima.net/api/public/v1'
  wx.showLoading({
    title: '加载中',
    mask: true,
  })
  return new Promise((resolve, reject) => {
    ajaxCount++
    wx.request({
      ...options,
      url: baseUrl + options.url,
      success: (result) => {
        resolve(result.data)
      },
      fail: (err) => {
        reject(err)
      },
      complete: () => {
        ajaxCount--
        if (ajaxCount === 0) {
          wx.hideLoading()
        }
      },
    })
  })
}
